package main;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

public class ItemsTester {

	private List<Item> itemsList = new ArrayList<Item>();
	private List<Integer> familleToTestList = new ArrayList<Integer>();
	
	public static void main(String[] args) throws Exception {
		ItemsTester itemTester = new ItemsTester();
		itemTester.genererTestList();
	}
	
	public ItemsTester(){
		familleToTestList.add(Constantes.ExalteMaraudeur);
	}

	public void genererTestList() throws Exception{
		GregorianCalendar greg = new GregorianCalendar();
		StringBuilder sb = new StringBuilder();
		List<Item> itemsListForRequest = new ArrayList<Item>();
		for (Item item : Item.values()){
			if (getFamilleToTestList().contains(item.getFamille())){
				itemsListForRequest.add(item);
			}
		}
		recupereValeursWs(itemsListForRequest);
		for (Item item : itemsListForRequest){
			sb.append(item.exportToBotV2ForTest());
		}
		System.out.println(sb.toString());
		TxtWriter writer = new TxtWriter("C:/Trader/" + getDate(greg) + "Tester.txt");
		writer.write(sb.toString());
	}

	public void initItem(Item item) {
		if (item.getBuy() != 0 && item.getSell() != 0){
			itemsList.add(item);
		}
	}
	
	public void recupereValeursWs(List<Item> items) throws Exception {
		List<List<Item>> itemsListOfList = new ArrayList<List<Item>>();
		for (int i=0; i<items.size()/50+1; i++){
			int lastIndex = (i+1)*50;
			if (items.size() < (i+1)*50){
				lastIndex = items.size();
			}
			itemsListOfList.add(items.subList(i*50, lastIndex));
		}
		for (List<Item> subItemsList : itemsListOfList){
			StringBuilder idListe = new StringBuilder();
			for (Item item : subItemsList){
				idListe.append(item.getId() + ",");
			}
			idListe.deleteCharAt(idListe.lastIndexOf(","));
			URL request1 = new URL("http://api.gw2tp.com/1/items?ids=" + idListe +
					"&fields=buy,sell");
			HttpURLConnection httpConnexion1 = (HttpURLConnection) request1.openConnection();
			httpConnexion1.addRequestProperty("User-Agent", "Mozilla/4.76");
			BufferedReader in = new BufferedReader(new InputStreamReader(httpConnexion1.getInputStream()));
			String inputLine;
			StringBuilder sb = new StringBuilder();
			while ((inputLine = in.readLine()) != null){
				sb.append(inputLine);
			}
			in.close();
			for (Item item : subItemsList){
				String currentItem = sb.substring(sb.indexOf("[" + item.getId() + ",") + 1, sb.indexOf("]", sb.indexOf("[" + item.getId() + ",")));
				if (!(item.getId() + "").equals((String) currentItem.split(",")[0])){
					throw new Exception("Echec de la récupération des datas");
				}
				item.setBuy(Integer.valueOf(currentItem.split(",")[1]));
				item.setSell(Integer.valueOf(currentItem.split(",")[2]));
			}
		}
	}
	
	private String getDate(GregorianCalendar greg){
		StringBuilder hour = new StringBuilder();
		if (greg.get(GregorianCalendar.MONTH) + 1 < 10){
			hour.append("0");
		}
		hour.append(greg.get(GregorianCalendar.MONTH) + 1);
		if (greg.get(GregorianCalendar.DAY_OF_MONTH) < 10){
			hour.append("0");
		}
		hour.append(greg.get(GregorianCalendar.DAY_OF_MONTH));
		if (greg.get(GregorianCalendar.HOUR_OF_DAY) < 10){
			hour.append("0");
		}
		hour.append(greg.get(GregorianCalendar.HOUR_OF_DAY));
		if (greg.get(GregorianCalendar.MINUTE) < 10){
			hour.append("0");
		}
		hour.append(greg.get(GregorianCalendar.MINUTE));
		if (greg.get(GregorianCalendar.SECOND) < 10){
			hour.append("0");
		}
		hour.append(greg.get(GregorianCalendar.SECOND));
		return hour.toString();
	}
	
	public List<Item> getItemsList() {
		return itemsList;
	}

	public void setItemsList(List<Item> itemsList) {
		this.itemsList = itemsList;
	}
	
	public List<Integer> getFamilleToTestList() {
    	return familleToTestList;
    }

	public void setFamilleToTestList(List<Integer> familleToTestList) {
    	this.familleToTestList = familleToTestList;
    }
	
}
